package com.jxl.demo.control;

import com.jxl.demo.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")

public class AdminController {
    @Autowired(required = false) //初始化对象
    private JdbcTemplate jdbc;


    //管理员登录
    //http://192.168.137.139:8080/adminLogin
    @PostMapping("/adminLogin")
    public Result adminLogin(@RequestBody Manager manager) {
        Result result = new Result();
        try {
            Manager admin = jdbc.queryForObject("SELECT * FROM manager WHERE name = ? AND pwd = ?",
                    new BeanPropertyRowMapper<>(Manager.class), manager.getName(), manager.getPwd());
            if (admin != null) {
                result.setCode(200);
                result.setResult(admin);
                System.out.println("管理员登录成功");
            } else {
                result.setCode(201);
                result.setResult("管理员账号或密码错误");
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
        }
        return result;
    }

    //管理员查询学生信息，使用stuNumber
    //http://192.168.137.139:8080/adminQuery
    @PostMapping("/adminQuery")
    public Result adminQuery(@RequestBody UserInfo userInfo){
        Result result = new Result();
        try {
            UserInfo queriedUserInfo = jdbc.queryForObject("select * from userinfo where stuNumber = ?",
                    new BeanPropertyRowMapper<>(UserInfo.class), userInfo.getStuNumber());
            if (queriedUserInfo != null) {
                result.setCode(200);
                result.setResult(queriedUserInfo);
                System.out.println("管理员查询学生信息成功");
            } else {
                result.setCode(404); // 使用适当的HTTP状态码（未找到）
                result.setResult("学生信息不存在");
                System.out.println("学生信息不存在");
            }
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
            return result;
        }
    }


    //管理员修改学生姓名,使用stuNumber判断
    //http://192.168.137.139:8080/adminChangeStuName
    @PostMapping("/adminChangeStuName")
    public Result adminChangeStuName(@RequestBody UserInfo userinfo){
        Result result = new Result();
        try {
            int rowsAffected = jdbc.update("update userinfo set name = ? where stuNumber = ?",
                    userinfo.getName(), userinfo.getStuNumber());
            if (rowsAffected > 0) {
                result.setCode(200);
                result.setResult("管理员修改学生姓名成功");
                System.out.println("管理员修改学生姓名成功");
            } else {
                result.setCode(404);
                result.setResult("学生信息不存在");
                System.out.println("学生信息不存在");
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
        }
        return result;
    }

    //管理员修改学生身份证,使用stuNumber判断
    //http://192.168.137.139:8080/adminChangeStuIdNum
    @PostMapping("/adminChangeStuIdNum")
    public Result adminChangeStuIdNum(@RequestBody UserInfo userinfo){
        Result result = new Result();
        try {
            int rowsAffected = jdbc.update("update userinfo set idNumber = ? where stuNumber = ?",
                    userinfo.getIdNumber(), userinfo.getStuNumber());
            if (rowsAffected > 0) {
                result.setCode(200);
                result.setResult("管理员修改学生身份证成功");
                System.out.println("管理员修改学生身份证成功");
            } else {
                result.setCode(404);
                result.setResult("学生信息不存在");
                System.out.println("学生信息不存在");
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
        }
        return result;
    }

    //管理员修改学生密码,使用stuNumber判断
    //http://192.168.137.139:8080/adminChangeStuPwd
    @PostMapping("/adminChangeStuPwd")
    public Result adminChangeStuPwd(@RequestBody UserInfo userinfo){
        Result result = new Result();
        try {
            int rowsAffected = jdbc.update("update userinfo set pwd = ? where stuNumber = ?",
                    userinfo.getPwd(), userinfo.getStuNumber());
            if (rowsAffected > 0) {
                result.setCode(200);
                result.setResult("管理员修改学生密码成功");
                System.out.println("管理员修改学生密码成功");
            } else {
                result.setCode(404);
                result.setResult("学生信息不存在");
                System.out.println("学生信息不存在");
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
        }
        return result;
    }


    //管理员删除指定学生信息，使用stuNumber
    //http://192.168.137.139:8080/adminDelete
    @PostMapping("/adminDelete")
    public Result adminDelete(@RequestBody UserInfo userinfo){
        Result result = new Result();
        try {
            int rowsAffected = jdbc.update("delete from userinfo where stuNumber = ?",
                    userinfo.getStuNumber());
            if (rowsAffected > 0) {
                result.setCode(200);
                result.setResult("删除学生信息成功");
                System.out.println("管理员删除指定学生信息成功");
            } else {
                result.setCode(404);
                result.setResult("学生信息不存在");
                System.out.println("学生信息不存在");
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
        }
        return result;
    }


    //管理员注册学生信息
    //http://192.168.137.139:8080/adminRegister
    @PostMapping("/adminRegister")
    public Result adminRegister(@RequestBody UserInfo userInfo){
        Result result = new Result();
        try {
            jdbc.update("insert into userinfo(idNumber, stuNumber, name, pwd, age, major) VALUES (?,?,?,?,?,?)",
                    userInfo.getIdNumber(),userInfo.getStuNumber(),userInfo.getName(),userInfo.getPwd(), userInfo.getAge(),userInfo.getMajor());
            result.setCode(200);
            result.setResult("管理员注册学生成功");
            System.out.println("管理员注册学生信息成功");
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
            return result;
        }
    }

    //管理员查看建议
    //http://192.168.137.139:8080/adminViewSuggestion
    @GetMapping("/adminViewSuggestion")
    public Result adminViewSuggestion(){
        Result result = new Result();
        try {
            List<Feedback> suggestions = jdbc.query("select * from feedback", new BeanPropertyRowMapper<>(Feedback.class));
            result.setCode(200);
            result.setResult(suggestions);
            System.out.println("管理员查看建议成功");
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
            return result;
        }
    }

    //管理员删除指定建议，用id来判断
    //http://192.168.137.139:8080/adminDelSuggestion
    @PostMapping("/adminDelSuggestion")
    public Result adminDelSuggestion(@RequestBody Feedback feedback) {
        Result result = new Result();
        try {
            jdbc.update("delete from feedback where id = ?", feedback.getId());
            result.setCode(200);
            result.setResult("管理员删除一条建议成功");
            System.out.println("管理员删除一条建议成功");
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
        }
        return result;
    }

    //管理员删除全部建议
    //http://192.168.137.139:8080/adminDelAllSuggestion
    @GetMapping("/adminDelAllSuggestion")
    public Result adminDelAllSuggestion() {
        Result result = new Result();
        try {
            jdbc.update("delete from feedback");
            result.setCode(200);
            result.setResult("管理员删除全部建议成功");
            System.out.println("管理员删除全部建议成功");
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
        }
        return result;
    }

    //管理员修改格言
    //http://192.168.137.139:8080/adminChangMotto
    @PostMapping("/adminChangMotto")
    public Result adminChangMotto(@RequestBody Manager manager) {
        Result result = new Result();
        try {
            jdbc.update("update manager set motto = ? where name = ?",
                    manager.getMotto(),
                    manager.getName());
            result.setCode(200);
            result.setResult("管理员修改格言成功");
            System.out.println("管理员修改格言成功");
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
        }
        return result;
    }

    //管理员修改密码
    //http://192.168.137.139:8080/adminChangePwd
    @PostMapping("/adminChangePwd")
    public Result adminChangePwd(@RequestBody AdminChangeInfo adminChangeInfo) {
        Result result = new Result();
        try {
            int rowsAffected = jdbc.update(
                    "update manager set pwd = ? where name = ? and pwd = ?",
                    adminChangeInfo.getNewPwd(),
                    adminChangeInfo.getManager().getName(),
                    adminChangeInfo.getManager().getPwd()
            );
            if (rowsAffected > 0) {
                result.setCode(200);
                result.setResult("管理员修改密码成功");
                System.out.println("管理员修改密码成功");
            } else {
                result.setCode(401); // 使用适当的HTTP状态码（未授权）
                result.setResult("旧密码错误或管理员信息不存在");
                System.out.println("旧密码错误或管理员信息不存在");
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
        }
        return result;
    }


    //管理员存入图片路径，使用id判断
    //http://192.168.137.139:8080/adminStoreImage
    @PostMapping("/adminStoreImage")
    public Result adminStoreImage(@RequestBody Manager manager){
        Result result = new Result();
        try {
            int rowsAffected = jdbc.update(
                    "update manager set imgAddress = ? where id = ?",
                    manager.getImgAddress(),
                    manager.getId()
            );
            if (rowsAffected > 0) {
                result.setCode(200);
                result.setResult("管理员图片地址存入成功");
                System.out.println("管理员图片地址存入成功");
            } else {
                result.setCode(404); // 使用适当的HTTP状态码（未找到）
                result.setResult("管理员信息不存在");
                System.out.println("管理员信息不存在");
            }
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
            return result;
        }
    }

}
